package io.github.codelifeleno.jdbrelease.execute;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.resource.ResourceUtil;
import cn.hutool.core.util.StrUtil;
import io.github.codelifeleno.jdbrelease.config.JDBReleaseConfig;
import io.github.codelifeleno.jdbrelease.model.JDBReleaseRunModel;
import org.junit.jupiter.api.Test;

import java.io.IOException;
import java.net.URISyntaxException;
import java.util.Date;

class JDBReleaseServiceTest {

    @Test
    void executeAll() throws IOException, URISyntaxException {
        String jdbReleaseConfigFilePath = "";
        if (jdbReleaseConfigFilePath.startsWith("classpath:")) {
            jdbReleaseConfigFilePath = jdbReleaseConfigFilePath.substring("classpath:".length());
        } else if (StrUtil.isBlank(jdbReleaseConfigFilePath)) {
            jdbReleaseConfigFilePath = "JDBReleaseConfig.xml";
        } else {
            throw new IllegalArgumentException("指定的文件路径不存在或不符合规范");
        }
        String runId = DateUtil.format(new Date(), "yyyyMMdd_HHmm");
        JDBReleaseRunModel runModel = new JDBReleaseRunModel(runId, ResourceUtil.getResourceObj(jdbReleaseConfigFilePath));
        JDBReleaseInit init = new JDBReleaseInit();
        JDBReleaseConfig jdbReleaseConfig = init.initConfig(ResourceUtil.getResourceObj(jdbReleaseConfigFilePath));
        JDBReleaseService jdbReleaseService = new JDBReleaseService(jdbReleaseConfig);
        jdbReleaseService.executeAll(runModel);
    }
}